package com.example.exception.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import sun.awt.ModalExclude;

/**
 * @ExceptionHandler + @ControllerAdvice 完成全局异常处理
 * 缺点：每种异常都要有自己的跳转方法
 */
@ControllerAdvice
public class GlobalException {

    /**
     * 在 @ExceptionHandler 所在类的类上添加 @ControllerAdvice注解之后，就能处理全局该类型的异常了
     * @param e
     * @return
     */
    @ExceptionHandler(value = {java.lang.ArithmeticException.class})
    public ModelAndView generateArithmeticException(Exception e){
        ModelAndView modelAndView = new ModelAndView("/exceptionPage");

        modelAndView.addObject("exception",e.toString());
        modelAndView.addObject("stackTrace",e.getStackTrace());
        modelAndView.addObject("errorInfo","算术异常");
        return modelAndView;
    }
}
